{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据归一化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 最值归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([85, 26,  4,  7, 42, 51, 29, 22, 96, 39, 49, 37, 80, 85,  3, 46, 84,\n",
       "       65, 73, 26, 76, 10, 17, 74, 26, 19, 87, 33, 27, 53, 95,  5, 12, 76,\n",
       "       17, 44, 57, 15, 69, 23,  8, 53, 28, 86, 94, 31, 76, 71, 33, 59, 29,\n",
       "       29, 38, 13, 70, 58, 36, 97, 44, 22, 54, 72, 71, 59, 97, 65, 48, 15,\n",
       "       77, 80,  9, 85, 71, 89, 58,  0, 15, 19, 45, 10, 79, 37,  0, 66, 68,\n",
       "       31, 54, 48, 47, 97,  8, 35, 68, 80,  2, 71, 58, 85, 11, 87])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.randint(0, 100, size=100)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.87628866, 0.26804124, 0.04123711, 0.07216495, 0.43298969,\n",
       "       0.5257732 , 0.29896907, 0.22680412, 0.98969072, 0.40206186,\n",
       "       0.50515464, 0.3814433 , 0.82474227, 0.87628866, 0.03092784,\n",
       "       0.4742268 , 0.86597938, 0.67010309, 0.75257732, 0.26804124,\n",
       "       0.78350515, 0.10309278, 0.17525773, 0.7628866 , 0.26804124,\n",
       "       0.19587629, 0.89690722, 0.34020619, 0.27835052, 0.54639175,\n",
       "       0.97938144, 0.05154639, 0.12371134, 0.78350515, 0.17525773,\n",
       "       0.45360825, 0.58762887, 0.15463918, 0.71134021, 0.2371134 ,\n",
       "       0.08247423, 0.54639175, 0.28865979, 0.88659794, 0.96907216,\n",
       "       0.31958763, 0.78350515, 0.73195876, 0.34020619, 0.60824742,\n",
       "       0.29896907, 0.29896907, 0.39175258, 0.13402062, 0.72164948,\n",
       "       0.59793814, 0.37113402, 1.        , 0.45360825, 0.22680412,\n",
       "       0.55670103, 0.74226804, 0.73195876, 0.60824742, 1.        ,\n",
       "       0.67010309, 0.49484536, 0.15463918, 0.79381443, 0.82474227,\n",
       "       0.09278351, 0.87628866, 0.73195876, 0.91752577, 0.59793814,\n",
       "       0.        , 0.15463918, 0.19587629, 0.46391753, 0.10309278,\n",
       "       0.81443299, 0.3814433 , 0.        , 0.68041237, 0.70103093,\n",
       "       0.31958763, 0.55670103, 0.49484536, 0.48453608, 1.        ,\n",
       "       0.08247423, 0.36082474, 0.70103093, 0.82474227, 0.02061856,\n",
       "       0.73195876, 0.59793814, 0.87628866, 0.11340206, 0.89690722])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x - np.min(x))/(np.max(x) - np.min(x) )   #最值归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.random.randint(0, 100, (50, 2))\n",
    "X = np.array(X, dtype = float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[40., 39.],\n",
       "       [61., 30.],\n",
       "       [97., 62.],\n",
       "       [99., 15.],\n",
       "       [21., 80.],\n",
       "       [ 4., 83.],\n",
       "       [33., 30.],\n",
       "       [95., 40.],\n",
       "       [94., 64.],\n",
       "       [98., 46.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X [:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[:, 0] = (X[:, 0] - np.min(X[:, 0]))/(np.max(X[:, 0] - np.min(X[:, 0])))  #第0列\n",
    "X[:, 1] = (X[:, 1] - np.min(X[:, 1]))/(np.max(X[:, 1] - np.min(X[:, 1])))  #第1列   有n列时用循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.39175258, 0.39175258],\n",
       "       [0.29896907, 0.29896907],\n",
       "       [0.62886598, 0.62886598],\n",
       "       [0.1443299 , 0.1443299 ],\n",
       "       [0.81443299, 0.81443299],\n",
       "       [0.84536082, 0.84536082],\n",
       "       [0.29896907, 0.29896907],\n",
       "       [0.40206186, 0.40206186],\n",
       "       [0.64948454, 0.64948454],\n",
       "       [0.46391753, 0.46391753]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 均值方差归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#继续使用 X \n",
    "X[:, 0] = (X[:,0] - np.mean(X[:, 0]))/np.std(X[:,0])  #第0列  mean 均值  std 方差\n",
    "X[:, 1] = (X[:,1] - np.mean(X[:, 1]))/np.std(X[:,1])  #第1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:,0])\n",
    "np.std(X[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
